package com.play.airfight;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

@Deprecated
public class MainActivity extends AppCompatActivity {

    float speed = 12f;
    PlaneView planeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        planeView = PlaneView.getPlaneView(this);

        setContentView(planeView);

        planeView.setBackgroundResource(R.drawable.fight_area);

        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();

        final int screenWidth = display.getWidth();
        final int screenHeight = display.getHeight();
        planeView.setWidthHeight(screenWidth, screenHeight);
        planeView.currentX = screenWidth / 2;
        planeView.currentY = screenHeight - 100;

        // 开启射击线程
        planeView.threadAddBullet.start();
        // 开启刷新子弹轨迹线程
        planeView.threadFly.start();
        // 开启增加敌人线程
        planeView.threadAddEnemy.start();

        // 键盘控制
        planeView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                switch (event.getKeyCode()) {
                    // 飞机下移
                    case KeyEvent.KEYCODE_DPAD_DOWN:
                        planeView.currentY += speed;
                        break;
                    //飞机上移
                    case KeyEvent.KEYCODE_DPAD_UP:
                        planeView.currentY -= speed;
                        break;
                    // 飞机左移
                    case KeyEvent.KEYCODE_DPAD_LEFT:
                        planeView.currentX -= speed;
                        break;
                    // 飞机右移
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                        planeView.currentX += speed;
                        break;
                }
                planeView.limitXY();
                Log.i("XY", "Cx: " + planeView.currentX + "  Cy:" + planeView.currentY);
                Log.i("XY", "Sx: " + screenWidth + "  Sy:" + screenHeight);
//                planeView.invalidate();
                return true;
            }
        });

        // 触摸控制
        planeView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float lastTouchX = 0F;
                float lastTouchY = 0F;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastTouchX = event.getX();
                        lastTouchY = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        float offSetX = event.getX() - planeView.currentX;
                        float offSetY = event.getY() - planeView.currentY;
                        // todo 差值器弹性滑动

                        planeView.currentX += (offSetX / 10) * 10;
                        planeView.currentY += (offSetY / 10) * 10;
                        break;
                    case MotionEvent.ACTION_UP:

                        break;
                }

                Log.i("touch_XY", "Tx: " + event.getX() + "  Ty:" + event.getY());
                Log.i("palne_XY", "Cx: " + planeView.currentX + "  Cy:" + planeView.currentY);

                return true;
            }
        });
    }

    // 退出时回收线程


//    @Override
//    protected void onStop() {
//        super.onStop();
//        // 开启射击线程
//        planeView.threadAddBullet.interrupt();
//        // 开启刷新子弹轨迹线程
//        planeView.threadFly.interrupt();
//        // 开启增加敌人线程
//        planeView.threadAddEnemy.interrupt();
//    }

//    @Override
//    protected void onNewIntent(Intent intent) {
//        super.onNewIntent(intent);
//        if (planeView != null) {
//            // 开启射击线程
//            planeView.threadAddBullet.start();
//            // 开启刷新子弹轨迹线程
//            planeView.threadFly.start();
//            // 开启增加敌人线程
//            planeView.threadAddEnemy.start();
//
//        }
//    }

}
